using UnityEngine;
using QFramework;
using UnityEngine.UI;
using UnityEngine.TextCore.Text;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
	public partial class ExpUpgrdelPlanel : UIElement,IController
	{
		
        private void Awake()
		{
			Init();
		}
		public void Init()
		{
			
			ResLoader loader = ResLoader.Allocate();
			var iconAtlas= loader.LoadSync<SpriteAtlas>("icon");
			Sprite simpleKnifeIcon;
			Sprite simpleKnifeicon;
			foreach (var expUpgradeItem in this.GetSystem<ExpUpgrdeSystem>().Items)
			{
				BtnExpUpgradeItemTemplate.InstantiateWithParent(UpgradeRoot)
				.Self(self =>
				{
					var itemCache = expUpgradeItem;
					this.Show();
					switch (expUpgradeItem.Description)
					{
						case "升级暴击概率": simpleKnifeIcon = iconAtlas.GetSprite("paired_bomb_icon"); break;
						case "升级炸弹": simpleKnifeIcon = iconAtlas.GetSprite("bomb_icon"); break;
						case "升级速度": simpleKnifeIcon = iconAtlas.GetSprite("movement_icon"); break;
						case "升级剑": simpleKnifeIcon = iconAtlas.GetSprite("simple_sword_icon"); break;
						case "升级匕首": simpleKnifeIcon = iconAtlas.GetSprite("paired_rotate_sword_icon"); break;
						case "升级锁": simpleKnifeIcon = iconAtlas.GetSprite("fly_icon"); break;
						case "升级刀": simpleKnifeIcon = iconAtlas.GetSprite("paired_simple_knife_icon"); break;
						case "升级篮球": simpleKnifeIcon = iconAtlas.GetSprite("ball_icon"); break;
						default: simpleKnifeIcon = iconAtlas.GetSprite("achievement_time_icon"); ; break;
					}
					switch (expUpgradeItem.Description)
					{
						case "升级暴击概率": simpleKnifeicon = iconAtlas.GetSprite("bomb"); break;
						case "升级炸弹": simpleKnifeicon = iconAtlas.GetSprite("bomb"); break;
						case "升级速度": simpleKnifeicon = iconAtlas.GetSprite("movement_icon"); break;
						case "升级剑": simpleKnifeicon = iconAtlas.GetSprite("simple_sword"); break;
						case "升级匕首": simpleKnifeicon = iconAtlas.GetSprite("rotate_sword"); break;
						case "升级锁": simpleKnifeicon = iconAtlas.GetSprite("fly_icon"); break;
						case "升级刀": simpleKnifeicon = iconAtlas.GetSprite("simple_knife"); break;
						case "升级篮球": simpleKnifeicon = iconAtlas.GetSprite("ball"); break;
						default: simpleKnifeicon = iconAtlas.GetSprite("achievement_time_icon"); ; break;
					}
					self.GetComponentInChildren<Text>().text = expUpgradeItem.Description + "(lv." + Global.Swordlv + ")" + "攻击力+" + 2 + "范围-" + 0.7f + "数量+" + 1;
					self.transform.Find("Icon").GetComponent<Image>().sprite = simpleKnifeIcon;
					self.transform.Find("nameicon").GetComponent<Text>().gameObject.GetComponentInChildren<Image>().sprite = simpleKnifeicon;
					self.name = expUpgradeItem.Description;
					self.onClick.AddListener(() =>
					{
						var itemCache = expUpgradeItem;
						self.onClick.AddListener(() =>
						{
							Time.timeScale = 1;
							itemCache.Upgrade();
							this.Hide();
							AudioKit.PlaySound("099_Achievement");
						});
						Time.timeScale = 1;
						if (expUpgradeItem != null)
						{
							expUpgradeItem.Upgrade();
							if (Global.Swordlv.Value % 2 == 0)
							{
								self.GetComponentInChildren<Text>().text = expUpgradeItem.Description + "(lv." + Global.Swordlv + ")" + "攻击力+" + 2 + "范围-" + 0.7f + "数量+" + 1;
							}
							else
							{
								self.GetComponentInChildren<Text>().text = expUpgradeItem.Description + "(lv." + Global.Swordlv + ")" + "攻击力+" + 3 + "范围-" + 0.9f + "数量+" + 1;
							}
							AudioKit.PlaySound("levelup");
							this.Hide();
						}
					});
					//}
					self.name = expUpgradeItem.Description;
					this.GetSystem<ExpUpgrdeSystem>().TypeItems.Add(self);
					//Debug.Log("Add"+this.GetSystem<ExpUpgrdeSystem>().TypeItems.Count);
				}).Show();
			}	
			// var btn = this.GetSystem<ExpUpgrdeSystem>().TypeItems;
			// 	Global.type = Random.Range(0, btn.Count);
			// 		for (int i = 0; i < btn.Count; i++)
			// 		{
			// 			if (btn[i] != null)
			// 			{
			// 				if (Global.type == i)
			// 				{
			// 					btn[i].Show();								
			// 				}
			// 				else
			// 				{
			// 					btn[i].Hide();
			// 				}
			// 			}
			// 		}
		}
		void OnEnable()
		{
			bool sf = false;
			var btns = this.GetSystem<ExpUpgrdeSystem>().TypeItems;
			for (int i = 0; i < btns.Count; i++)
			{
				if (btns[i] == null)
				{
					sf = true;
					break;
				}
			}
			if (sf)
			{
				for (int i = 0; i < btns.Count; i++)
				{
					if (btns[i] != null)
					{
						Destroy(btns[i].gameObject);
					}
				}
				this.GetSystem<ExpUpgrdeSystem>().TypeItems.Clear();
				Init();
			}
        }
        protected override void OnBeforeDestroy()
		{
		}

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;//初始化（确保系统注册）。
        }

    }
}
